package com.way.fileexplorer.activities;

import android.util.Log;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

import com.way.fileexplorer.adapters.FileListAdapter;

public class FileListScrollListener implements OnScrollListener {
    
    private FileListAdapter mAdapter;
    private int mVisibleLastIndex = 0;
    private static final String TAG = "FileListScrollListener";
    public FileListScrollListener(FileListAdapter adapter){
        mAdapter = adapter;
    }
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
            int totalItemCount) {
        mVisibleLastIndex = firstVisibleItem + visibleItemCount - 1;
        
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        Log.d(TAG, "onScrollStateChanged mVisibleLastIndex = " + mVisibleLastIndex
              + " onScrollStateChanged mAdapter.getCount() = " + (mAdapter.getCount()-1));
        if (mVisibleLastIndex == mAdapter.getCount()-1 &&
            scrollState == OnScrollListener.SCROLL_STATE_IDLE ){
            mAdapter.loadMoreIfNecessary();
        }
    } 
}
